Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mixin for handling partial i18n objects #8558

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sissbruecker
Copy link
Contributor

@sissbruecker sissbruecker commented Jan 23, 2025

Adds a mixin for handling partial I18N objects in components:

  • The mixin receives a default I18N properties object on initialization that is provided by each component.
  • The mixin then initializes the component's custom I18N properties with a copy of the defaults, which ensures backward compatibility with the current behavior where every component returns the default values from the i18n property.
  • When setting a properties object through the i18n property it is possible to provide a partial I18N object. The mixin will then do a deep merge of the default properties with the custom properties to ensure that the effective I18N object has a value for every property.
  • In order to use this in individual components, those need to be refactored to use the new __effectiveI18n property internally.

The benefits would be:

  • Web component users could provide partial objects containing only the translations they want to override, keeping the defaults for the rest
  • In some Flow components we already have support for partial objects, which requires custom logic in every component. This could then be simplified to just use the i18n property.
  • This also facilitates adding support for global I18N in that the mixin could be extended to also include a global I18N object for the respective component in the deep merge mechanism.

Copy link

* localization, replace this with an object that provides all properties, or
* just the individual properties you want to change.
*/
i18n: DeepPartial<I>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change for Typescript users in that the following would not compile anymore:

const i18n: DatePickerI18n = datePicker.i18n;

Seems kind of niche?.

OTOH other common use cases like the following should still work:

datePicker.i18n = { ...(new DatePicker().i18n), months: [...] };

Another option to applying the deep partial type in the mixin could be to apply it to the respective I18N type in each component, so if someone would import DatePickerI18n for example, they would already get a partial type. That could also make it easier for type-checks in application code that wants to use a partial object in some state. For example, the following would not compile with the current solution, but would compile if we changed DatePickerI18n to be partial itself.

const i18n = useSignal<DatePickerI18n>({ months: [...] })

@sissbruecker sissbruecker marked this pull request as ready for review January 24, 2025 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant